﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
//using storeSMS.DBUtility;//Please add references
namespace storeSMS.DAL
{
	/// <summary>
	/// 数据访问类:sendinfo
	/// </summary>
	public partial class sendinfo
	{
		public sendinfo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("ID", "sendinfos"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from sendinfos");
			strSql.Append(" where ID="+ID+" ");
			return DbHelperOleDb.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(storeSMS.Model.sendinfos model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.sid != null)
			{
				strSql1.Append("sid,");
				strSql2.Append(""+model.sid+",");
			}
			if (model.sendinfo != null)
			{
				strSql1.Append("sendinfo,");
				strSql2.Append("'"+model.sendinfo+"',");
			}
			if (model.sendtime != null)
			{
				strSql1.Append("sendtime,");
				strSql2.Append("'"+model.sendtime+"',");
			}
			if (model.response != null)
			{
				strSql1.Append("response,");
				strSql2.Append("'"+model.response+"',");
			}
			if (model.rtime != null)
			{
				strSql1.Append("rtime,");
				strSql2.Append("'"+model.rtime+"',");
			}
			if (model.senduserid != null)
			{
				strSql1.Append("senduserid,");
				strSql2.Append(""+model.senduserid+",");
			}
			if (model.sendnum != null)
			{
				strSql1.Append("sendnum,");
				strSql2.Append("'"+model.sendnum+"',");
			}
			strSql.Append("insert into sendinfos(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(storeSMS.Model.sendinfos model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sendinfos set ");
			if (model.sid != null)
			{
				strSql.Append("sid="+model.sid+",");
			}
			else
			{
				strSql.Append("sid= null ,");
			}
			if (model.sendinfo != null)
			{
				strSql.Append("sendinfo='"+model.sendinfo+"',");
			}
			else
			{
				strSql.Append("sendinfo= null ,");
			}
			if (model.sendtime != null)
			{
				strSql.Append("sendtime='"+model.sendtime+"',");
			}
			else
			{
				strSql.Append("sendtime= null ,");
			}
			if (model.response != null)
			{
				strSql.Append("response='"+model.response+"',");
			}
			else
			{
				strSql.Append("response= null ,");
			}
			if (model.rtime != null)
			{
				strSql.Append("rtime='"+model.rtime+"',");
			}
			else
			{
				strSql.Append("rtime= null ,");
			}
			if (model.senduserid != null)
			{
				strSql.Append("senduserid="+model.senduserid+",");
			}
			else
			{
				strSql.Append("senduserid= null ,");
			}
			if (model.sendnum != null)
			{
				strSql.Append("sendnum='"+model.sendnum+"',");
			}
			else
			{
				strSql.Append("sendnum= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where ID="+ model.ID+"");
			int rowsAffected=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        public bool UpdateR(string phone,string content,DateTime dt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sendinfos set ");

            strSql.Append("response='" + content + "',");

            strSql.Append("rtime=#" + dt.ToString()+"#");

            //strSql.Append(" where sendnum='" + phone + "' and sendtime<#"+dt.ToString()+"#");
            strSql.Append(" where ID in(select top 1 ID from sendinfos where sendnum='" + phone + "')");
            int rowsAffected = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sendinfos ");
			strSql.Append(" where ID="+ID+"" );
			int rowsAffected=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sendinfos ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public storeSMS.Model.sendinfos GetModel(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append(" ID,sid,sendinfo,sendtime,response,rtime,senduserid,sendnum ");
			strSql.Append(" from sendinfos ");
			strSql.Append(" where ID="+ID+"" );
			storeSMS.Model.sendinfos model=new storeSMS.Model.sendinfos();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}       
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public storeSMS.Model.sendinfos DataRowToModel(DataRow row)
		{
			storeSMS.Model.sendinfos model=new storeSMS.Model.sendinfos();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["sid"]!=null && row["sid"].ToString()!="")
				{
					model.sid=int.Parse(row["sid"].ToString());
				}
				if(row["sendinfo"]!=null)
				{
					model.sendinfo=row["sendinfo"].ToString();
				}
				if(row["sendtime"]!=null && row["sendtime"].ToString()!="")
				{
					model.sendtime=DateTime.Parse(row["sendtime"].ToString());
				}
				if(row["response"]!=null)
				{
					model.response=row["response"].ToString();
				}
				if(row["rtime"]!=null && row["rtime"].ToString()!="")
				{
					model.rtime=DateTime.Parse(row["rtime"].ToString());
				}
				if(row["senduserid"]!=null && row["senduserid"].ToString()!="")
				{
					model.senduserid=int.Parse(row["senduserid"].ToString());
				}
				if(row["sendnum"]!=null)
				{
					model.sendnum=row["sendnum"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT sendinfos.sid, sendinfos.sendinfo, sendinfos.sendtime, sendinfos.response, sendinfos.rtime, sendinfos.senduserid, sendinfos.sendnum, storeinfo.gys, storeinfo.project, storeinfo.projectbh, storeinfo.cgddh, storeinfo.phone, storeinfo.qr, storeinfo.gh, storeinfo.nums FROM sendinfos LEFT JOIN storeinfo ON sendinfos.sid = storeinfo.ID ");
			
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where 1=1 "+strWhere);
			}
            strSql.Append(" order by sendtime desc ");
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sendinfos ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperOleDb.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from sendinfos T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

